package com.jhws.business.car.controller;

import com.jhws.common.base.bo.JsonRespData;
import com.jhws.business.car.bo.Discount;
import com.jhws.business.car.service.DiscountService;
import com.jhws.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/car/discount")
public class DiscountController {
    @Resource
    private DiscountService discountService;

    @RequestMapping("saveDiscount.x")
    @ResponseBody
    public JsonRespData saveDiscount(Discount discount) {
        List<Discount> discounts = discountService.queryDiscounts("where park_id='" + discount.getParkId()
                + "' and discount_type='" + discount.getDiscountType() + "' and start_month='" + discount.getStartMonth() + "'");
        if (discounts != null && discounts.size() > 0) {
            discount.setId(discounts.get(0).getId());
        }
        if (discount.getDiscount() == null || discount.getDiscount() == 0) {
            discountService.deleteDiscount(discount);
        } else {
            discountService.saveDiscount(discount);
        }
        return new JsonRespData("", 1, "");
    }

    @RequestMapping("queryDiscountList.x")
    @ResponseBody
    public JsonRespData queryDiscountList(Discount discount) {
        String where = "where 1=1 ";
        if (StringUtils.isNotEmpty(discount.getParkId())) {
            where += " and park_id='" + discount.getParkId() + "'";
        }
        if (StringUtils.isNotEmpty(discount.getDiscountType())) {
            where += " and discount_type='" + discount.getDiscountType() + "'";
        }
        if (discount.getStartMonth() != null && discount.getStartMonth() != 0) {
            where += " and start_month='" + discount.getStartMonth() + "'";
        }
        return new JsonRespData("", 1, discountService.queryDiscounts(where));
    }

    @RequestMapping("queryDiscountMax.x")
    @ResponseBody
    public JsonRespData queryDiscountMax(String parkId, Integer month) {
        String where = "where start_month=(select max(startMonth) from Discount where park_id='" + parkId + "' and start_month <= '" + month + "') and park_id='" + parkId + "'";
        List<Discount> discounts = discountService.queryDiscounts(where);
        return new JsonRespData("", 1, discounts != null && discounts.size() > 0 ? discounts.get(0) : null);
    }
}
